package com.ruoyi.web.controller.qcsystem;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qcsystem.domain.entity.QualityConfirmation;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.qcsystem.service.QcDataService;
import com.ruoyi.ruoyictmanage.domain.entity.Line;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("qcSystem")
public class QCSystemController {

    @Autowired
    QcDataService qcDataService;
    Integer lineId;
    String startTime;
    String endTime;
    String mpn;
    String lot;

    @GetMapping("list")
    PageInfo<QualityConfirmation> qualityConfirmationList(@RequestParam(defaultValue="6")Integer lineId,
                                                          @RequestParam(defaultValue="1")Integer pageNum,
                                                          @RequestParam(defaultValue="10")Integer pageSize,
                                                          @RequestParam(defaultValue="")String startTime,
                                                          @RequestParam(defaultValue="")String endTime,
                                                          @RequestParam(defaultValue="")String mpn,
                                                          @RequestParam(defaultValue="")String lot
    ){
        if (!startTime.isEmpty() && !endTime.isEmpty()) {
            if (startTime.equals(endTime)) {
                try {
                    DateTimeFormatter ft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDate date = LocalDate.parse(endTime, ft);
                    LocalDate nextDay = date.plusDays(1);
                    endTime = nextDay.format(ft);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        this.lineId = lineId==null?1:lineId;
        this.startTime = startTime==null?"":startTime;
        this.endTime = endTime==null?"":endTime;
        this.mpn = mpn==null?"":mpn;
        this.lot = lot==null?"":lot;
        return qcDataService.qualityConfirmationList(lineId,pageNum,pageSize,startTime,endTime,mpn,lot);
    }

    @GetMapping("lineList")
    List<Line> lineList(){
        return qcDataService.lineList();
    }

    @PostMapping("addQualityConfirmation")
    int addQualityConfirmation(@Validated  @RequestBody QualityConfirmation qualityConfirmation){
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String userName = SecurityUtils.getLoginUser().getUsername();
        qualityConfirmation.setUserId(userId);
        qualityConfirmation.setAdmit(userName);
        qualityConfirmation.setProcessId(1);
        return qcDataService.addQualityConfirmation(qualityConfirmation);
    }

    @DeleteMapping("deleteQualityConfirmation")
    int deleteQualityConfirmation(@RequestParam Long qcId){
        return qcDataService.deleteQualityConfirmation(qcId);
    }

    @GetMapping("getMachineNumber")
    int getMachineNumber(@RequestParam Integer lineId){
        long processId=1;
        return qcDataService.getMachineNumber(lineId,processId);
    }

    @PostMapping("updateQualityConfirmation")
    int updateQualityConfirmation(@Validated @RequestBody QualityConfirmation qualityConfirmation){
        return qcDataService.updateQualityConfirmation(qualityConfirmation);
    }

    @GetMapping("confirmQualityConfirmation")
    int confirmQualityConfirmation(@RequestParam Long qcId){
        String confirmer = SecurityUtils.getLoginUser().getUsername();
        int count= qcDataService.getQualityConfirmationCountByUserId(SecurityUtils.getLoginUser().getUserId(),qcId);
        if (count>0){
            return 0;
        }else {
            return qcDataService.confirmQualityConfirmation(qcId,confirmer);
        }
    }


    @GetMapping("handleExport")
    List<QualityConfirmation> handleExport(){
        return qcDataService.qualityConfirmationList(lineId,startTime,endTime,mpn,lot);
    }

    @PostMapping("getMpn14ByLot")
    ResponseEntity<String> getMpn14ByLot(@Validated @RequestBody List<String> lotList){
        String targetUrl = "http://192.168.131.43:8006/commapi/as400/v0/getMpn14ByLot";
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("lotList", lotList);

        // 创建请求实体
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);

        // 发送请求
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(targetUrl, requestEntity, String.class);
        return  stringResponseEntity;
    }


}
